// index.js
// 获取应用实例
const app = getApp()
import {
    util,
    Toast,
    commonMixin
} from "../../utils/util.js";

Page({
    behaviors: [commonMixin],
    data: {
        showPopup: false,
        scrollTop: 0,
        userInfo: {
            avatar: '',
            avatarUrl: '',
            name: '',
            status: 0,
            is_verifier: 0,
        },
        questionCategories: [],
    },
    onLoad() {
        wx.showLoading({
            title: '加载中',
          })
          util.http.post("v1/mini/question-categories",{}).then((res) => {
              if (res.data.code == 0) {
                this.setData({
                    questionCategories: res.data.data,
                })
              }
              wx.hideLoading();
          }).catch((err) => {
              wx.hideLoading();
              Toast('网络异常！刷新后重试！');
              console.log(err)
          })
    },
    onShow() {
      if (app.globalData.loginFlag) {
        Toast('请先登录后再操作！');
        app.globalData.loginFlag = false;
      }
        util.getMysqlUser();
        let userInfo = util.getUserInfo()
        this.setData({
            userInfo: {
                name: userInfo.name ? userInfo.name : '未知',
                avatarUrl: userInfo.avatar_file ? userInfo.avatar_file.url : this.data.commonFile + "5.png",
                status: userInfo.status,
                is_verifier: userInfo.is_verifier,
            }
        });
    },

    onShareAppMessage() {
       
    },

    handleWriteOff() {
        wx.scanCode({  
            success: function (res) {  
                if (res.result.indexOf('https://wechat.lychix.com/s/') != -1) {
                    let sampleSn = res.result.replace('https://wechat.lychix.com/s/', '');
                    wx.showLoading({
                        title: '加载中',
                    })
                    util.http.post("v1/mini/sample-scan-write-off",{
                        sample_sn: sampleSn
                    }).then((res) => {
                        if (res.data.code == 0) {
                            Toast('核销成功！');
                        } else {
                            Toast(res.data.message);
                        }
                        wx.hideLoading();
                    }).catch((err) => {
                        wx.hideLoading();
                        Toast('网络异常！刷新后重试！');
                        console.log(err)
                    })
                }
            },  
            fail: function (err) {  
                Toast('扫码出错！');
            }  
        });
    },

    handleJumpQuestion(event) {
        let {
            id
          } = event.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/question/index?id=' + id,
          })
    },

    handleCheckLogin() {
        wx.navigateTo({
          url: '/pages/register/index',
        })
    }
})